package com.seo.controller.transaction;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.github.pagehelper.PageInfo;
import com.seo.function.PageTool;
import com.seo.service.bill.sdk.BillService;
import com.seo.service.keyword.sdk.KeywordService;
import com.seo.service.proxy.sdk.BillProxyService;
import com.seo.utils.ClientConst;
import com.seo.utils.Jurisdiction;

import lombok.extern.slf4j.Slf4j;

/**
 * @ClassName: BillController
 * @author rtc
 * @version 2018.07.28 12:20
 */
@Slf4j
@Controller
@RequestMapping(value = "/bill/")
public class BillUserController {

	
	@Resource(name = "billService")
	private BillService billService;
	
	@Resource(name = "billProxyService")
	private BillProxyService billProxyService;
	
	@Resource(name = "keywordService")
	private KeywordService keywordService;
	
	@Autowired
	private PageTool pageTool;
	
	/**
	 * 余额明细
	 * @param params params
	 * @param request request
	 * @return String
	 */
	@RequestMapping(value = "/balance")
	public String balanceList(@RequestParam Map<String, Object> params, HttpServletRequest request) throws Exception {
		log.info("余额明细");
		Integer userId = Jurisdiction.getT("id", Integer.class, ClientConst.SESSION_USER);
		if(params == null || params.isEmpty()) {
			params = new HashMap<>();
		}
		if(Jurisdiction.roleCode("proxy")) {
			request.setAttribute("proxy", "01");
		} else {
			request.setAttribute("proxy", "02");
		}
		params.put("userId", userId);
		PageInfo<?> page = pageTool.getAutoPage(billService::getBalanceList, params);
		request.setAttribute("page", page);
		request.setAttribute("param", params);
		request.setAttribute("userId", userId);
		return "transaction/balance_list";
	}
	
	/**
	 * 余额明细
	 * @param params params
	 * @param request request
	 * @return String
	 */
	@RequestMapping(value = "proxy/balance")
	public String proxyBalanceList(@RequestParam Map<String, Object> params, HttpServletRequest request) throws Exception {
		log.info("余额明细");
		Integer userId = Jurisdiction.getT("id", Integer.class, ClientConst.SESSION_USER);
		params.put("userId", userId);
		PageInfo<?> page = pageTool.getAutoPage(billProxyService::getProxyBalanceList, params);
		request.setAttribute("page", page);
		request.setAttribute("param", params);
		return "transaction/proxy/balance_list";
	}
	
	/**
	 * 下级余额明细
	 * @param params params
	 * @param request request
	 * @return String
	 */
	@RequestMapping(value = "child/balance")
	public String childBalanceList(@RequestParam Map<String, Object> params, HttpServletRequest request) throws Exception {
		log.info("下级余额明细");
		Integer userId = Jurisdiction.getT("id", Integer.class, ClientConst.SESSION_USER);
		params.put("parentId", userId);
		PageInfo<?> page = pageTool.getAutoPage(billService::getBalanceList, params);
		request.setAttribute("page", page);
		request.setAttribute("param", params);
		return "transaction/child/balance_list";
	}
	
	/**
	 * 关键词扣费列表
	 * @param params params
	 * @param request request
	 * @return String
	 */
	@RequestMapping(value = "/keyWordDeduc")
	public String keyWordDeduc(@RequestParam Map<String, Object> params, HttpServletRequest request) throws Exception {
		log.info("关键词扣费记录");
		Integer userId = Jurisdiction.getT("id", Integer.class, ClientConst.SESSION_USER);
		if(params == null || params.isEmpty()) {
			params = new HashMap<>();
		}
		if(Jurisdiction.roleCode("proxy")) {
			request.setAttribute("proxy", "01");
		} else {
			request.setAttribute("proxy", "02");
		}
		List<Map<String, Object>> engine = keywordService.getEngineList();
		params.put("userId", userId);
		PageInfo<?> page = pageTool.getAutoPage(billService::getKeyWordDeducList, params);
		request.setAttribute("page", page);
		request.setAttribute("param", params);
		request.setAttribute("engine", engine);
		request.setAttribute("userId", userId);
		return "transaction/keyWord_deduc_list";
	}
	
	/**
	 * 新站关键词扣费记录
	 * @param params params
	 * @param request request
	 * @return String
	 */
	@RequestMapping(value = "/newStationKeyWordDeduc")
	public String newStationKeyWordDeduc(@RequestParam Map<String, Object> params, HttpServletRequest request) throws Exception {
		log.info("新站关键词扣费记录");
		Integer userId = Jurisdiction.getT("id", Integer.class, ClientConst.SESSION_USER);
		if(params == null || params.isEmpty()) {
			params = new HashMap<>();
		}
		if(Jurisdiction.roleCode("proxy")) {
			request.setAttribute("proxy", "01");
		} else {
			request.setAttribute("proxy", "02");
		}
		List<Map<String, Object>> engine = keywordService.getEngineList();
		params.put("userId", userId);
		PageInfo<?> page = pageTool.getAutoPage(billService::getNewKeyWordDeducList, params);
		request.setAttribute("page", page);
		request.setAttribute("param", params);
		request.setAttribute("engine", engine);
		request.setAttribute("userId", userId);
		return "transaction/new_keyWord_deduc_list";
	}
	
	/**
	 * 关键词扣费列表
	 * @param params params
	 * @param request request
	 * @return String
	 */
	@RequestMapping(value = "proxy/keyWordDeduc")
	public String proxyKeyWordDeduc(@RequestParam Map<String, Object> params, HttpServletRequest request) throws Exception {
		log.info("关键词扣费记录");
		List<Map<String, Object>> engine = keywordService.getEngineList();
		Integer userId = Jurisdiction.getT("id", Integer.class, ClientConst.SESSION_USER);
		params.put("userId", userId);
		
		PageInfo<?> page = pageTool.getAutoPage(billProxyService::getKeyWordDeducList, params);
		request.setAttribute("page", page);
		request.setAttribute("engine", engine);
		request.setAttribute("param", params);
		return "transaction/proxy/keyWord_deduc_list";
	}
	
	/**
	 * 下级关键词扣费列表
	 * @param params params
	 * @param request request
	 * @return String
	 */
	@RequestMapping(value = "child/keyWordDeduc")
	public String childKeyWordDeduc(@RequestParam Map<String, Object> params, HttpServletRequest request) throws Exception {
		log.info("下级关键词扣费记录");
		List<Map<String, Object>> engine = keywordService.getEngineList();
		Integer userId = Jurisdiction.getT("id", Integer.class, ClientConst.SESSION_USER);
		params.put("parentId", userId);
		
		PageInfo<?> page = pageTool.getAutoPage(billService::getKeyWordDeducList, params);
		request.setAttribute("page", page);
		request.setAttribute("engine", engine);
		request.setAttribute("param", params);
		return "transaction/child/keyWord_deduc_list";
	}
	
	/**
	 * 下级新站关键词扣费记录
	 * @param params params
	 * @param request request
	 * @return String
	 */
	@RequestMapping(value = "child/newStationKeyWordDeduc")
	public String childNewStationKeyWordDeduc(@RequestParam Map<String, Object> params, HttpServletRequest request) throws Exception {
		log.info("下级新站关键词扣费记录");
		Integer userId = Jurisdiction.getT("id", Integer.class, ClientConst.SESSION_USER);
		List<Map<String, Object>> engine = keywordService.getEngineList();
		params.put("parentId", userId);
		PageInfo<?> page = pageTool.getAutoPage(billService::getNewKeyWordDeducList, params);
		request.setAttribute("page", page);
		request.setAttribute("param", params);
		request.setAttribute("engine", engine);
		return "transaction/child/new_keyWord_deduc_list";
	}
}
